package com.lenovo.leos.filebrowser.engine.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.res.XmlResourceParser;

public class MimeTypeParser {

    private static final String LOG_TAG = "MimeTypeParser";

    public static final String TAG_MIMETYPES = "MimeTypes";
    public static final String TAG_TYPE = "type";
    public static final String TAG_DIR = "dir";
    
    public static final String ATTR_EXTENSION = "extension";
    public static final String ATTR_MIMETYPE = "mimetype";
    public static final String ATTR_DEFAULTFOLDER="defaultfolder";
    
    private XmlPullParser mXpp;
    private MimeTypes mMimeTypes;

    public MimeTypeParser() {
    }
    
    public MimeTypes fromXml(InputStream in)
                    throws XmlPullParserException, IOException {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

            mXpp = factory.newPullParser();
            mXpp.setInput(new InputStreamReader(in));

            return parse();
    }
    
    public MimeTypes fromXmlResource(XmlResourceParser in) throws XmlPullParserException, IOException {
            mXpp = in;
            
            return parse();
    }

    public MimeTypes parse() throws XmlPullParserException, IOException {
            
            mMimeTypes = new MimeTypes();
            
            int eventType = mXpp.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                    String tag = mXpp.getName();

                    if (eventType == XmlPullParser.START_TAG) {
                            if (tag.equals(TAG_MIMETYPES)) {
                                    
                            } else if (tag.equals(TAG_TYPE)) {
                                    addMimeTypeStart();
                            }
                            else if (tag.equals(TAG_DIR)){
                                    addDefaultFolderStart();
                            }
                    } else if (eventType == XmlPullParser.END_TAG) {
                            if (tag.equals(TAG_MIMETYPES)) {
                                    
                            }
                    }

                    eventType = mXpp.next();
            }

            return mMimeTypes;
    }
    private void addDefaultFolderStart(){
            Log.d(LOG_TAG,"addDefaultFolderStart");
            String defaultfolder = mXpp.getAttributeValue(null, ATTR_DEFAULTFOLDER);
            String mimetype = mXpp.getAttributeValue(null, ATTR_MIMETYPE);
            mMimeTypes.put(mimetype,defaultfolder);
    }
    
    private void addMimeTypeStart() {
            //return name
            String extension = mXpp.getAttributeValue(null, ATTR_EXTENSION);
            String mimetype = mXpp.getAttributeValue(null, ATTR_MIMETYPE);
            mMimeTypes.put(extension, mimetype);
    }
    
}
